import csv
import json
import init_api_attribute


def analysis_params():
    params = {"caseList":[{"execution":"0","auto":"no","keywords":"captcha","module":"282","precondition":"1","type":"feature","scene":"0","casestep":[{"testcaseId":14,"expect":"1","type":"step","version":"1","testcaseTitle":"新建用户","desc":"1"}],"testcase_title":"新建用户","id":14,"status":"blocked"},{"execution":"0","auto":"no","keywords":"login_normal","module":"282","precondition":"1","type":"feature","scene":"0","casestep":[{"testcaseId":14,"expect":"1","type":"step","version":"1","testcaseTitle":"新建用户","desc":"1"}],"testcase_title":"新建用户","id":14,"status":"blocked"}],"code":200,"exeEnviroment":"prod","isSuccess":"true","jenkinsJob":"auto-test","msg":"查询用例成功","needNewPackage":"true","testTaskId":3}

    case_list = params.get('caseList')
    test_task_id = params.get('testTaskId')

    # 将testTaskId写入到全局配置文件中
    init_api_attribute.save_test_taskid(str(test_task_id))

    print('--------------------------------------------------------')

    params_path = '../params.csv'
    for item in case_list:
        print(item)
        # 写入数据到csv文件中
        keywords = item.get('keywords')
        with open(params_path, 'w', newline='', encoding='utf-8') as csvfile:
            keynames = []
            for key in item.keys():
                keynames.append(key)

            keynames.append('expect')
            keynames.append('actual_res')
            keynames.append('is_pass')

            #写表头
            writer = csv.DictWriter(csvfile, fieldnames=keynames)
            writer.writeheader()
            writer.writerows(case_list)
        break
        # f = open(csv_file_path, 'r', encoding='utf-8')
        # reader = csv.DictReader(f)
        # for line in reader:


def analysis_params_json(params):
    case_list = params.get('caseList')
    test_task_id = params.get('testTaskId')
    init_api_attribute.save_test_taskid(test_task_id)

    params_path = '../params.csv'
    for item in case_list:
        print(item)
        # 写入数据到csv文件中
        keywords = item.get('keywords')

        # 根据keywords关键字筛选

        with open(params_path, 'w', newline='', encoding='utf-8') as csvfile:
            keynames = []
            for key in item.keys():
                keynames.append(key)

            keynames.append('expect')
            keynames.append('actual_res')
            keynames.append('is_pass')

            # 写表头
            writer = csv.DictWriter(csvfile, fieldnames=keynames)
            writer.writeheader()
            writer.writerows(case_list)
        break
    return testTaskId


def test_analysis():
    analysis_params()
